<template>
  <div class="log-console">
    <div class="log-header">
      <h3>运行日志</h3>
      <el-button size="small" @click="$emit('clear-logs')">清除日志</el-button>
    </div>
    <div class="log-content" ref="logContainer">
      <div v-for="(log, index) in logs" :key="index" class="log-item" :class="getLogLevelClass(log)">
        <span class="log-timestamp">{{ log.timestamp }}</span>
        <span class="log-level" :class="`level-${log.level || 'info'}`">
          {{ formatLogLevel(log.level) }}
        </span>
        <span class="log-message">{{ log.message }}</span>
      </div>
      <div v-if="logs.length === 0" class="empty-logs">
        <span>暂无日志</span>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue';

export default {
  name: 'LogConsole',
  props: {
    logs: {
      type: Array,
      default: () => []
    }
  },
  emits: ['clear-logs'],
  setup(props) {
    const logContainer = ref(null);

    // 监听日志变化，自动滚动到底部
    watch(() => props.logs.length, () => {
      setTimeout(() => {
        if (logContainer.value) {
          logContainer.value.scrollTop = logContainer.value.scrollHeight;
        }
      }, 100);
    });

    // 根据日志级别返回对应的CSS类
    const getLogLevelClass = (log) => {
      const level = log.level || 'info';
      return `log-${level}`;
    };

    // 格式化日志级别显示
    const formatLogLevel = (level) => {
      if (!level) return '[INFO]';
      
      // 转换为大写并添加方括号
      return `[${level.toUpperCase()}]`;
    };

    return {
      logContainer,
      getLogLevelClass,
      formatLogLevel
    };
  }
};
</script>

<style scoped>
.log-console {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  overflow: hidden;
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #dcdfe6;
}

.log-header h3 {
  margin: 0;
  font-size: 16px;
  color: #303133;
}

.log-content {
  height: 300px;
  overflow-y: auto;
  padding: 10px;
  background-color: #fafafa;
  font-family: 'Courier New', Courier, monospace;
}

.log-item {
  padding: 5px;
  margin-bottom: 5px;
  border-radius: 3px;
  font-size: 14px;
  line-height: 1.4;
  display: flex;
  flex-wrap: wrap;
}

/* 日志级别样式 */
.log-info {
  background-color: #f0f9ff;
  border-left: 3px solid #409eff;
}

.log-warning, .log-warn {
  background-color: #fdf6ec;
  border-left: 3px solid #e6a23c;
}

.log-error {
  background-color: #fef0f0;
  border-left: 3px solid #f56c6c;
}

.log-debug {
  background-color: #f5f5f5;
  border-left: 3px solid #909399;
}

.log-critical, .log-fatal {
  background-color: #fef0f0;
  border-left: 3px solid #cf1322;
  font-weight: bold;
}

.log-timestamp {
  color: #606266;
  margin-right: 8px;
  min-width: 150px;
}

.log-level {
  font-weight: bold;
  margin-right: 8px;
  min-width: 60px;
}

/* 日志级别文字颜色 */
.level-info {
  color: #409eff;
}

.level-warning, .level-warn {
  color: #e6a23c;
}

.level-error {
  color: #f56c6c;
}

.level-debug {
  color: #909399;
}

.level-critical, .level-fatal {
  color: #cf1322;
}

.log-message {
  white-space: pre-wrap;
  word-break: break-word;
  flex: 1;
}

.empty-logs {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  color: #909399;
}
</style>